package common.db;


import common.db.RowProcessor;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * User: Kostylev Alexey
 * Date: 16.02.2007
 * Time: 18:28:51
 */
public abstract class ScalarProcessor<E> implements RowProcessor<E>
{
	public static <E> RowProcessor<E> getInstance(final Class<E> clazz, final int index)
	{
		return new RowProcessor<E>()
		{
			public E processRow(ResultSet rs)
			{
				try
				{
					if (clazz.equals(String.class))
						return (E)rs.getString(index);
					else if (clazz.equals(Integer.class))
						return (E)(Integer)rs.getInt(index);

					// todo add other scalar types

					throw new IllegalArgumentException("Unknown scalar type: " + clazz);

				}
				catch (SQLException e)
				{
					throw new RuntimeException(e);
				}
			}
		};
	}
	public static <E> RowProcessor<E> getInstance(final Class<E> clazz)
	{
		return getInstance(clazz, 1);
	}

}
